<template>
  <base-dialog ref="dialog" title="确认回厂">
    <template #default="{ close }">
      <uni-forms ref="form" :border="true" :model="form" label-position='top' label-width="150"
                 style="width: 100%;">
        <uni-forms-item :rules="[{ required: true, errorMessage: '不能为空'}]" label="回厂时间"
                        name="returnTime" required>
          <uni-datetime-picker ref="NowEndPick" v-model="form.returnTime" :border="false"
                               :clear-icon="false" :input-border="false" returnType="String"
                               type="datetime" value="defaultDateTime" @change="changeLog"/>
        </uni-forms-item>
        <uni-forms-item :rules="[{ required: true, errorMessage: '不能为空' }]" label="车牌号" name="vehicleId"
                        required>
          <base-picker v-model="form.vehicleId" :localdata="vehicleList"/>
        </uni-forms-item>
        <uni-forms-item
            :rules="[{ required: true, errorMessage: '不能为空' }, { pattern: /^\d{1,8}\.?\d{0,2}$/, errorMessage: '格式不正确' }]"
            label="地磅重量"
            name="weighbridge" required>
          <uni-easyinput v-model="form.weighbridge" :input-border="false" placeholder="请输入"
                         type="digit">
            <template #right>kg</template>
          </uni-easyinput>
        </uni-forms-item>
        <uni-forms-item class="returnImgs"
                        label="上传图片" label-width="150" name="returnImgs">
          <base-images v-model="form.returnImgs" :limit="3"></base-images>
        </uni-forms-item>

        <view class="btn-group" style="display: flex; align-items: center; justify-content: space-between;">
          <!-- <button class="def-btn" @click="submit(toComplete)">当日任务完成</button>
          <button class="main-btn" @click="submit()">回厂继续出车</button> -->
		  <view class="main-btn middle" style="width: 100%" @click="submit()">确认回厂</view>
        </view>
      </uni-forms>
    </template>
  </base-dialog>
</template>

<script>
import {TaskPlanApi as Api} from "@/comms/api/taskPlanApi";
import {DateUtil, Msg, toPath} from "@/utils/BaseUtils";

export default {
  data() {
    return {
      form: {},
      vehicleList: []
    };
  },
  emits: ["done"],
  methods: {
    async open(data = {}) {
      await Api.taskPlanVehicleList({taskPlanId: data.id}).then(({data}) => {
        this.vehicleList = (data || []).map((val = {}) => ({value: val.vehicleId, text: val.licensePlate}))
      })
      this.form = {taskPlanId: data.id};
      this.form.returnTime = DateUtil.getCurrDateTime();
      this.$refs.dialog.open()
    },
    async validate(handler) {
      this.$refs.form.validate()
          .then(() => {
            handler && handler(() => this.$refs.dialog.close());
          }).catch(() => ({}))
    },

    toComplete() {
      this.$refs.dialog.close()
      toPath("/pages/tasks/plannedRoute/completeTask?id=" + this.form.taskPlanId)
    },

    submit(handler) {
      this.validate(close => {
        const {form = {}} = this;
        Api.returnFactory({...form}).then(res => {
          handler?.();
          close?.();
          Msg.success("确认回厂成功");
          this.$emit("done")
        })
      });
    }
  }
}
</script>

<style lang="scss">
	.main-btn{
		width: 330rpx;
		height: 100rpx;
		background: #1763fc;
		border-radius: 10rpx;
		font-size: 30rpx;
		color: #fff;
	}
	.def-btn{
		width: 330rpx;
		height: 100rpx;
		// background: #1763fc;
		border: 1px solid #1763fc;
		border-radius: 10rpx;
		font-size: 30rpx;
		color: #1763fc;
	}
</style>
